package com.tjp.eight.ui.fragment.tabhome;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.baoyz.widget.PullRefreshLayout;
import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.WApplication;
import com.tjp.eight.ui.activity.ActivityFragmentDetail;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.ToatUtils;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/13.
 */
public class FragmentHomeType extends BaseFragment<HomeTypePresenter> implements HomeTypeView {

    private View contentView;
    private RecyclerView recyclerView;
    private ArrayList<HomeListModel.HomeListItemModel> homeListModels;
    private int type = 0;
    private String caId = "";
    private int currentPage = 1;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    private HomeAdapter homeAdapter;
    private View footView;
    private ProgressBar progressBarFoot;
    private TextView textViewFoot;
    private PullRefreshLayout prl_refresh;
    private EndlessRecyclerOnScrollListener endlessRecyclerOnScrollListener;

    public void setCaId(String caId) {
        this.caId = caId;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_home_type, container, false);
        }
        initViews();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        String json = getJson();
        presenter.loadData(json);
    }

    @Override
    protected HomeTypePresenter createPresenter() {
        return new HomeTypePresenter(this);
    }

    @Override
    public void loadSuccess(HomeListModel homeListModel) {
        if (homeListModel == null) return;
        if (IsSuccessUtils.isSuccess(homeListModel.result)) {
            homeAdapter.addData(homeListModel);
            prl_refresh.setRefreshing(false);

            if (homeListModel.data == null || homeListModel.data.size() < 4) {
                textViewFoot.setText("没有更多了");
                progressBarFoot.setVisibility(View.GONE);
            } else {
                textViewFoot.setText("加载更多...");
                progressBarFoot.setVisibility(View.VISIBLE);
            }
        } else {
            //homeListModel.msg 提示
        }
    }

    @Override
    public void loadFail(String msg) {
        ToatUtils.toastCommentFail(getContext(), msg);
    }

    @Override
    public void loadMSuccess(HomeListModel homeListModel) {
        if (homeListModel == null) return;
        if (IsSuccessUtils.isSuccess(homeListModel.result)) {
            homeAdapter.addData(homeListModel.data);

            if (homeListModel.data == null || homeAdapter.getItemCount() >= homeListModel.total_items) {
                textViewFoot.setText("没有更多了");
                progressBarFoot.setVisibility(View.GONE);
            } else {
                textViewFoot.setText("加载更多...");
                progressBarFoot.setVisibility(View.VISIBLE);
            }
        } else {
            //homeListModel.msg 提示
        }
    }

    private String getJson() {
        HomeListPostModel homeListPostModel = new HomeListPostModel();
        homeListPostModel.caid = caId;
        homeListPostModel.page = currentPage;
        Gson gson = new Gson();
        String json = gson.toJson(homeListPostModel);
        return json;
    }

    void initViews() {
        homeListModels = new ArrayList<>();
        recyclerView = (RecyclerView) contentView.findViewById(R.id.rv_home_type);
        prl_refresh = (PullRefreshLayout) contentView.findViewById(R.id.prl_refresh);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        homeAdapter = new HomeAdapter(WApplication.getwApplication());

        homeAdapter.addData(homeListModels);
        homeAdapter.setType(type);
        homeAdapter.setHomeItemOnClickListener(new HomeAdapter.HomeItemOnClickListener() {
            @Override
            public void onClick(HomeListModel.HomeListItemModel homeModel, int position) {
                String caid = homeModel.cid;
                ActivityUtils.startDetails(getActivity(), caid);

            }
        });
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(homeAdapter);
        footView = LayoutInflater.from(getActivity()).inflate(R.layout.swipe_refresh_footer, recyclerView, false);
        progressBarFoot = (ProgressBar) footView.findViewById(R.id.pb_foot_load);
        textViewFoot = (TextView) footView.findViewById(R.id.tv_foot_tips);
        headerViewRecyclerAdapter.addFooterView(footView);
        recyclerView.setAdapter(headerViewRecyclerAdapter);
        endlessRecyclerOnScrollListener = new EndlessRecyclerOnScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int cpage) {
                Log.e("pageFHT", caId + "::" + cpage);
                currentPage = cpage;
                String json = getJson();
                presenter.loadNData(json);
            }
        };
        recyclerView.addOnScrollListener(endlessRecyclerOnScrollListener);
        prl_refresh.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                endlessRecyclerOnScrollListener.reSet();
                currentPage = 1;
                String json = getJson();
                presenter.loadData(json);
            }
        });
    }
}
